import Vue from 'vue'
import VueRouter from 'vue-router'

if (process.env.NODE_ENV === 'development') {
  Vue.use(VueRouter)
}

import Layout from '@/layout'
import saleRouter from './modules/saleRouter'
import purchaseRouter from './modules/purchaseRouter'
import rpRouter from './modules/rpRouter'
import presaleRouter from './modules/presaleRouter'
import storeRouter from './modules/storeRouter'
import priceRouter from './modules/priceRouter'
import tableRouter from './modules/tableRouter'
import hrRouter from './modules/hr'
import inventory from './modules/inventory'
import monitorRouter from './modules/monitor'
import visitRouter from './modules/visitRouter'
import maintainRouter from './modules/maintainRouter'
import initRouter from './modules/initRouter'
import mallRouter from './modules/mallRouter'
import permission from './modules/permission'
import sysRouter from './modules/sys'
import { voucherRouter, accbookRouter, reportRouter, periodRouter } from './modules/account'
import setRouter from './modules/setRouter'
export const constantRoutes = [{
  path: '/redirect',
  component: Layout,
  hidden: true,
  children: [{
    path: '/redirect/:path*',
    component: resolve => require(['@/views/redirect/index'], resolve)
  }]
}, {
  path: '/login',
  component: resolve => require(['@/views/login/index'], resolve),
  hidden: true,
  children: [{
    path: '/login',
    component: resolve => require(['@/views/login/login'], resolve),
    hidden: true
  }, {
    path: '/forgetPsd',
    component: resolve => require(['@/views/login/forgetPsd'], resolve),
    hidden: true
  }, {
    path: '/forgetCode',
    component: resolve => require(['@/views/login/forgetCode'], resolve),
    hidden: true
  }, {
    path: '/register',
    component: resolve => require(['@/views/login/register'], resolve),
    hidden: true
  }]
}, {
  path: '/404',
  component: resolve => require(['@/views/error/404'], resolve),
  hidden: true
}, {
  path: '/',
  component: Layout,
  redirect: '/home',
  children: [{
    path: '/home',
    component: resolve => require(['@/views/home/index'], resolve),
    name: 'Home',
    meta: { title: '主页', icon: 'home', affix: true }
  }, {
    path: '/accountinfo',
    component: resolve => require(['@/views/home/account'], resolve),
    name: 'AccountInfo',
    hidden: true,
    meta: { title: '账户信息' }
  }, {
    path: '/help',
    component: resolve => require(['@/views/home/help'], resolve),
    name: 'UseHelp',
    hidden: true,
    meta: { title: '帮助' }
  }]
}]

export const asyncRoutes = [
  purchaseRouter,
  saleRouter,
  rpRouter,
  presaleRouter,
  storeRouter,
  inventory,
  priceRouter,
  visitRouter,
  voucherRouter,
  accbookRouter,
  periodRouter,
  reportRouter,
  tableRouter,
  hrRouter,
  setRouter,
  initRouter,
  maintainRouter,
  permission,
  sysRouter,
  monitorRouter,
  mallRouter,
  {
    path: '*',
    edirect: '/404',
    hidden: true
  }
]

const createRouter = () => new VueRouter({
  // mode: 'history',
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher
}

export default router
